<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加服务</title>
</head>
<body>
<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i>
    首页
    <span class="c-gray en">&gt;</span>
    基础设置
    <span class="c-gray en">&gt;</span>
    服务配置
    <span class="c-gray en">&gt;</span>
    添加服务
</nav>
<div class="page-container">
    <div class="form-unit">基本信息</div>
    <form action="" method="post" class="form form-horizontal" id="merchant-service-form-add">
        <div class="form-group">
            <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>服务名称：</label>
            <div class="formControls col-xs-4 col-sm-4">
                <input type="text" class="form-control" value="" placeholder="必填项" id="BasicServiceAdd-serviceName" name="serviceName">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>服务类型：</label>
            <div class="formControls col-xs-4 col-sm-4">
                <input type="text" class="form-control" placeholder="" name="serviceType" id="BasicServiceAdd-serviceType">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>工时：</label>
            <div class="formControls col-xs-4 col-sm-4 form-inline">
                <input type="text" class="form-control" value="" placeholder="必填项" name="workingHours" id="BasicServiceAdd-workingHours" style="width: 200px">&nbsp;&nbsp;分钟
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>服务单价：</label>
            <div class="formControls col-xs-4 col-sm-4">
                <input type="text" class="form-control" value="" placeholder="必填项" name="price" id="BasicServiceAdd-price">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>打折价格：</label>
            <div class="formControls col-xs-4 col-sm-4">
                <input type="text" class="form-control" value="" placeholder="必填项" name="discountPrice" id="BasicServiceAdd-discountPrice">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>服务简介：</label>
            <div class="formControls col-xs-4 col-sm-4">
                <input type="text" class="form-control" value="" placeholder="必填项" name="serviceDesc" id="BasicServiceAdd-serviceDesc">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>服务内容：</label>
            <div class="formControls col-xs-4 col-sm-4">
                <textarea rows="4" maxlength="500" class="form-control " name="serviceContent" id="BasicServiceAdd-serviceContent"></textarea>
            </div>
        </div>
        <div class="form-group">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <input onclick="merchantBasicServiceAddFun.submitAnaysis();return false;" class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;"/>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript">
    /*服务类型配置，添加页面 merchantBasicServiceAddFun*/
    var merchantBasicServiceAddFun;

    merchantBasicServiceAddFun={
        init : function () {
            let _this = this;
            _this.bootstrapvalidatorForm();

            //加载可选服务小类型
            $.ajax({
                type: "GET",
                url: "biz/getServiceTypeByBizId",
                dataType: "json",
                success: function(data){
                    if(data.state){
                        $("#BasicServiceAdd-serviceType").select2({
                            data: data.data,
                            placeholder:'请选择类型',
                            width : '195px'
                        });
                        $('#BasicServiceAdd-serviceType').on('select2:select', function (e) {
                            $(e.target).siblings('I').removeClass('glyphicon glyphicon-remove').addClass('glyphicon glyphicon-ok').show().siblings('SMALL').hide().parents('.form-group').removeClass('has-error').addClass('has-success')
                        });
                        $('#BasicServiceAdd-serviceType').on('select2:unselect', function (e) {
                            $(e.target).siblings('I').removeClass('glyphicon glyphicon-ok').addClass('glyphicon glyphicon-remove').show().siblings('SMALL').show().parents('.form-group').removeClass('has-success').addClass('has-error')
                        });
                    }

                },
                error:function () {
                    alert("error2");
                }
            });
        },
        //提交
        submitAnaysis : function () {
            //bootstrap验证
            var bootstrapValidator = $("#merchant-service-form-add").data('bootstrapValidator');
            bootstrapValidator.validate();
            if(bootstrapValidator.isValid())
                $("#merchant-service-form-add").submit();
            else return;

            //将元转为分
            var temp = $("#merchant-service-form-add").serializeArray();
            for(var i =0;i<temp.length;i++){
                if(temp[i].name == 'price'){
                    temp[i].value = temp[i].value*100;
                }
                if(temp[i].name == 'discountPrice'){
                    temp[i].value = temp[i].value*100;
                }
            }
            $.ajax({
                type: "GET",
                url: "biz/insertServiceByBizId",
                data: temp,
                dataType: "json",
                success: function(data){
                    //state为true，新增成功
                    if(data.state){
                        layer.msg(data.message, {icon: 1});
                        //关闭新增窗口
                        removeIframe();

                        //重新查询一次表格数据
                        $.ajax({
                            type: "get",
                            url: "biz/getServiceByBizId",
                            dataType:"json",
                            success : function(json) {
                                $("#basic-service").bootstrapTable('load', json);
                            }
                        })
                    }

                },
                error:function () {
                    alert("error");
                }
            });
        },
        //bootstrap验证
        bootstrapvalidatorForm: function () {
            $('#merchant-service-form-add').bootstrapValidator({
                message: 'This value is not valid',
                excluded : [':disabled'],
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    serviceName: {
                        validators: {
                            notEmpty: {
                                message: '服务名称不能为空'
                            },
                            stringLength: {
                                min: 2,
                                max: 100,
                                message: '服务名称长度必须在2到100位之间'
                            }
                        }
                    },
                    serviceType: {
                        validators: {
                            notEmpty: {
                                message: '服务类型不能为空'
                            }
                        }
                    },
                    price: {
                        validators: {
                            notEmpty: {
                                message: '服务单价不能为空'
                            },
                            regexp: {
                                regexp: /^((^([1-9]\d*))|^0)(\.\d{1,2})?$|(^[-]0\.\d{1,2}$)$/,
                                message: '服务单价格式不正确'
                            },
                            stringLength: {
                                min: 1,
                                max: 5,
                                message: '服务单价长度必须在1到5位之间'
                            }
                        }
                    },
                    workingHours: {
                        validators: {
                            notEmpty: {
                                message: '工时不能为空'
                            },
                            regexp: {
                                regexp: /^(?!0)\d{1,3}$/,
                                message: '工时格式不正确'
                            }
                        }
                    },
                    discountPrice: {
                        validators: {
                            notEmpty: {
                                message: '打折价格不能为空'
                            },
                            regexp: {
                                regexp: /^((^([1-9]\d*))|^0)(\.\d{1,2})?$|(^[-]0\.\d{1,2}$)$/,
                                message: '打折价格格式不正确'
                            },
                            stringLength: {
                                min: 1,
                                max: 5,
                                message: '打折价格长度必须在1到5位之间'
                            }
                        }
                    },
                    serviceDesc: {
                        validators: {
                            notEmpty: {
                                message: '服务简介不能为空'
                            },
                            stringLength: {
                                min: 4,
                                max: 200,
                                message: '服务简介长度必须在4到200位之间'
                            }
                        }
                    },
                    serviceContent: {
                        validators: {
                            notEmpty: {
                                message: '服务内容不能为空'
                            },
                            stringLength: {
                                min: 6,
                                max: 500,
                                message: '服务内容长度必须在6到500位之间'
                            }
                        }
                    }
                }
            });
        }
    };

    merchantBasicServiceAddFun.init();
</script>
</body>
</html>